package com.ohs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ohs.mapper.before.ProMapper;
import com.ohs.pojo.Product;
import com.ohs.pojo.Product_Comment_User;
import com.ohs.pojo.ShopCar;
import com.ohs.service.ProService;
import com.ohs.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ProServiceImpl implements ProService {
    @Autowired(required = false)
    ProMapper proMapper;

    @Override
    public String findVaseProList(Model model) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("proType","花瓶花器");
        List<Product> vaseList = proMapper.selectList(queryWrapper);
        model.addAttribute("vaseProList",vaseList);
        return "before/vase_proList";
    }

    /* 	<!--三表查询（商品表、评价表、用户表）-->*/
    @Override
    public String findProDetail(Model model,Integer proId) {
        if (proId==null){
            System.out.println("传过来的商品id为空");
            return null;
        }
        List<Product_Comment_User> comments = proMapper.findProDetail(proId);
        Product pro = proMapper.findPro(proId);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isHot",1);
        //順便查找热销商品，ishot为1的商品
        List<Product> hotPros = proMapper.selectList(queryWrapper);
        model.addAttribute("comments",comments);
        model.addAttribute("pro",pro);
        model.addAttribute("hotPros",hotPros);
        return "before/proDetail";
    }

    /*查找所有商品*/
    @Override
    public String findAllPro(Model model, HttpSession session,String param) {
        if (param==null){
            param="防止首次查询出现空指针";
        }
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
/*        queryWrapper.eq(param=="priceLowToHigh","price",param);
        queryWrapper.eq(param=="priceHighToLow","price",param);
        queryWrapper.eq(param=="saleLowToHigh","account",param);
        queryWrapper.eq(param=="saleHighToLow","account",param);*/
        if (param.equals("priceLowToHigh")){
            queryWrapper.orderByAsc("price");
            List<Product> allPro = proMapper.selectList(queryWrapper);
            if (allPro==null){
                System.out.println("未查询到商品");
            }
            model.addAttribute("allPro",allPro);
            return "before/allPro";
        }
         if (param.equals("priceHighToLow")){
            queryWrapper.orderByDesc("price");
            List<Product> allPro = proMapper.selectList(queryWrapper);
            if (allPro==null){
                System.out.println("未查询到商品");
            }
            model.addAttribute("allPro",allPro);
            return "before/allPro";
        }
         if (param.equals("saleLowToHigh")){
            queryWrapper.orderByAsc("account");
            List<Product> allPro = proMapper.selectList(queryWrapper);
            if (allPro==null){
                System.out.println("未查询到商品");
            }
            model.addAttribute("allPro",allPro);
            return "before/allPro";
        }
         if (param.equals("saleHighToLow")){
            queryWrapper.orderByDesc("account");
            List<Product> allPro = proMapper.selectList(queryWrapper);
            if (allPro==null){
                System.out.println("未查询到商品");
            }
            model.addAttribute("allPro",allPro);
            return "before/allPro";
        }
            List<Product> allPro = proMapper.selectList(queryWrapper);
            if (allPro==null){
                System.out.println("未查询到商品");
            }
            model.addAttribute("allPro",allPro);
            return "before/allPro";

    }
    //加入购物车

    @Override
    public ResponseResult addShopCar(Product pro, Integer quantity, HttpSession session) {
        //根据商品id和用户id查询购物车中有木有这件商品，如果有，则向已存在的数量追加，否则就直接插入一条数据
        //首先获取userId，如果为空即未登录或者会话已过期
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId==null){
            return new ResponseResult(201,"请先登录");
        }
        //根据商品id和用户id查询该用户的购物车中有木有这件商品,
/*      因为耦合所以用不了plus
        QueryWrapper<ShopCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("proId",pro.getProId());*/
        ShopCar existedPro = proMapper.findProByProIDAndUserId(userId, pro.getProId());
        //如果商品已存在，则向该商品追加购买
        if (existedPro!=null){
            int i = proMapper.addQuantity(pro.getProId(), userId, quantity);
            if (i==1){
                return new ResponseResult(200,"成功加购 "+quantity+"件");
            }
        }
        //如果根据俩id查出来的购物车记录为空，则说明该用户尚未购买此商品
        int i = proMapper.addShopCar(pro.getProId(), userId, quantity);
        if (i==1){
            return new ResponseResult(200,"加入购物车成功！数量："+quantity+"件");
        }
        return new ResponseResult(202,"未知错误！");
    }

    @Override
    public List<Product> findProByName(String proName, Model model) {
        if (proName==null||proName==""){
            System.out.println("请输入商品名");
        }
        List<Product> proList = proMapper.findProByName(proName);
        System.out.println(proList);
        if (proList==null){
            System.out.println("未查询到商品");
        }
        return proList;
    }

    @Override
    public String findIdeaPro(Model model, HttpSession session, String param) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("proType","创意家居");
        List<Product> ideaList = proMapper.selectList(queryWrapper);
        if (ideaList==null){
            System.out.println("未查询到创意家居！");
        }
        model.addAttribute("ideaList",ideaList);
        return "before/idea";
    }

    @Override
    public String findSoftDecoration(Model model, HttpSession session, String param) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("proType","布艺软饰");
        List<Product> softDecoration = proMapper.selectList(queryWrapper);
        if (softDecoration==null){
            System.out.println("未查询到布艺软饰！");
        }
        model.addAttribute("softDecoration",softDecoration);
        return "before/decoration";
    }

    @Override
    public String findPerfume(Model model, HttpSession session, String param) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("proType","蜡艺香薰");
        List<Product> perfume = proMapper.selectList(queryWrapper);
        if (perfume==null){
            System.out.println("未查询到蜡艺香薰！");
        }
        model.addAttribute("perfume",perfume);
        return "before/perfume";
    }


}
